package com.certificationManager.server.domain.dao.dom4j;

import java.io.File;

import org.apache.log4j.Logger;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.certificationManager.server.domain.dao.QuestionDAO;

public class QuestionDAODom4j implements QuestionDAO {

	private static final QuestionDAODom4j singleton = new QuestionDAODom4j();
	
	public static QuestionDAODom4j getInstance() {return singleton;}
	
	/** A Bo should never be instantiated */
	private QuestionDAODom4j() {}
	
	/** Logger */
	private static final Logger logger = Logger.getLogger(QuestionDAODom4j.class); 
	
	/**
	 *  This Method Init all Questions in database.
	 */
	public void initQuestions() {
		
		try {
			File file = new File("questions/Q1.xml");
			SAXReader reader = new SAXReader();
			Document document;
			document = reader.read(file);
			Element root = document.getRootElement();
			int questionID = Integer.valueOf((String)root.attribute("id").getData());
			int version = new Integer(root.selectSingleNode("version").getText()).intValue();
			String theme = root.selectSingleNode("theme").getText();
			String type = root.selectSingleNode("type").getText();
			String question = root.selectSingleNode("question").getText();
			Node responses = root.selectSingleNode("responses");
			for (Node response:responses.selectNodes("response")) {
				String value = response.selectSingleNode("value").getText();
				boolean result = new Boolean(response.selectSingleNode("result").getText()).booleanValue();
			}
		} catch (DocumentException e) {
			logger.error(e.getMessage(),e);
		}
	}
}
